<?php
//说明当前页面输出的是图片
header('Content-type:image/jpeg');
$width=120;
$height=40;
$string='';
$img=imagecreatetruecolor($width, $height);//建一个真彩色图像
$arr=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
//生成彩色像素
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
//填充函数，xy确定坐标，color颜色执行区域填充颜色
imagefill($img, 0, 0, $colorBg);

//生成干扰点
for($m=0;$m<=100;$m++){
    $pointcolor=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));//点的颜色
    imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),$pointcolor);// 水平地画一串像素点
}
//画干扰直线
for ($i=0;$i<=4;$i++){
    $linecolor=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));//线的颜色
    imageline($img,rand(0,$width),rand(0,$height),rand(0,$width),rand(0,$height),$linecolor);//画一条线段
}
for($i=0;$i < 4;$i++){
    $string.=$arr[rand(0,count($arr)-1)];
}
session_start();//将验证码保存到session中
$_SESSION['vcode'] = $string;
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
//绘制文本
imagestring($img,10,rand(0,$width-36),rand(0,$height-15),$string,$colorString);
//输出图片
imagejpeg($img);
imagedestroy($img);
?>